package com.nbstar.film.spider.font.woff.table.glyf;

import com.nbstar.film.spider.font.woff.AbstractOTFDataStream;
import com.nbstar.film.spider.font.woff.utils.BoundingBox;

import java.awt.geom.GeneralPath;
import java.io.IOException;

/**
 * Created with IntelliJ IDEA.
 *
 * @author: wangxindong
 * @date: 2018/9/2-09:40
 */
public class GlyphData {

	private short numberOfContours;
	private short xMin;
	private short yMin;
	private short xMax;
	private short yMax;

	private BoundingBox boundingBox;

	private AbstractGlyfDescript glyphDescription = null;

	public void initData(GlyphTable glyphTable, AbstractOTFDataStream data, int leftSideBearing) throws IOException {
		numberOfContours = data.readSignedShort();
		xMin = data.readSignedShort();
		yMin = data.readSignedShort();
		xMax = data.readSignedShort();
		yMax = data.readSignedShort();
		boundingBox = new BoundingBox(xMin, yMin, xMax, yMax);

		if (numberOfContours >= 0) {
			// create a simple glyph
			short x0 = (short) (leftSideBearing - xMin);
			glyphDescription = new GlyfSimpleDescript(numberOfContours, data, x0);
		} else {
			// create a composite glyph
			glyphDescription = new GlyfCompositeDescript(data, glyphTable);
		}
	}

	public short getNumberOfContours() {
		return numberOfContours;
	}

	public void setNumberOfContours(short numberOfContours) {
		this.numberOfContours = numberOfContours;
	}

	public short getxMin() {
		return xMin;
	}

	public void setxMin(short xMin) {
		this.xMin = xMin;
	}

	public short getyMin() {
		return yMin;
	}

	public void setyMin(short yMin) {
		this.yMin = yMin;
	}

	public short getxMax() {
		return xMax;
	}

	public void setxMax(short xMax) {
		this.xMax = xMax;
	}

	public short getyMax() {
		return yMax;
	}

	public void setyMax(short yMax) {
		this.yMax = yMax;
	}

	public GlyphDescription getDescription() {
		return glyphDescription;
	}

	public GeneralPath getPath() {
		return new GlyphRenderer(glyphDescription).getPath();
	}
	
	public BoundingBox getBoundingBox() {
		return boundingBox;
	}

	public void setBoundingBox(BoundingBox boundingBox) {
		this.boundingBox = boundingBox;
	}

	@Override
	public String toString() {
		return "GlyphData{" + "numberOfContours=" + numberOfContours + ", xMin=" + xMin + ", yMin=" + yMin + ", xMax="
				+ xMax + ", yMax=" + yMax + ", glyphDescription=" + glyphDescription + ", boundingBox=" + boundingBox
				+ '}';
	}
}
